package com.zh.api.controller.operation;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.service.UserService;
import com.zh.common.utils.*;
import com.zh.operation.domain.OlderInOutDO;
import com.zh.operation.service.OlderInOutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 每日老人托管记录
 * 
 * @author beichen
 * @email 1992lcg@163.com
 * @date 2018-12-06 19:50:22
 */
 
@Controller
@Api("托管【每日老人记录】")
public class OlderInController extends BaseController {
	@Autowired
	private OlderInOutService olderInOutService;

	@Log("托管今日老人签到记录")
	@ResponseBody
	@RequestMapping(value = "listOperationOlderInOut", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationOlderInOut(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
		Query query = new Query(params);
		List<OlderInOutDO> inOutList = olderInOutService.list(query);
		int total = olderInOutService.count(query);
		PageUtils pageUtils = new PageUtils(inOutList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("查询所有老人托管信息")
	@ResponseBody
	@RequestMapping(value = "listAllOperationOlderInOut", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listAll(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
		Query query = new Query(params);
		List<OlderInOutDO> inOutList = olderInOutService.listAll(query);
		int total = olderInOutService.count1(query);
		PageUtils pageUtils = new PageUtils(inOutList, total);
		return ResultVO.success(pageUtils);
	}

	@Log("编辑托管记录")
	@ResponseBody
	@RequestMapping(value = "editOperationOlderInOutById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationOlderInOutById(@RequestBody  OlderInOutDO inOut){
		if(inOut==null || CheckEmptyUtil.isEmpty(inOut.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		OlderInOutDO olderInOutDO = olderInOutService.get(inOut.getId());
	    return ResultVO.success(olderInOutDO);
	}
	@Log("新增托管记录")
	@ResponseBody
	@RequestMapping(value = "saveOperationOlderInOut", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationOlderInOut(@RequestBody OlderInOutDO inOut){
		if(inOut==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		inOut.setCreateBy(getUserId());//创建热ID
		if(olderInOutService.save(inOut)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	@Log("更新托管记录")
	@ResponseBody
	@RequestMapping(value = "updateOperationOlderInOutById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationOlderInOutById(@RequestBody OlderInOutDO inOut){
		if(inOut==null || CheckEmptyUtil.isEmpty(inOut.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		olderInOutService.update(inOut);
		return ResultVO.success();
	}
	@Log("删除托管记录")
	@ResponseBody
	@RequestMapping(value = "removeOperationOlderInOutById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationOlderInOutById(@RequestBody OlderInOutDO inOut){
		if(inOut==null || CheckEmptyUtil.isEmpty(inOut.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(olderInOutService.remove(inOut.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}

	@RequestMapping(value = "batchRemoveOperationOlderInOutByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除托管记录")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(olderInOutService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
}
